import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'

export default defineConfig({
  plugins: [vue(), vueJsx(), vueDevTools()],
  server: {
    host: 'localhost',
    port: 5173,
    cors: true, // 确保启用CORS
    proxy: {
      '/api': {
        target: 'http://8.138.5.119:8088',
        changeOrigin: true, 
        rewrite: (path) => path,
        secure: false,
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('🚨 代理错误:', err.message)
          })
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('➡️  代理请求:', req.method, req.url)
            proxyReq.setHeader('Origin', 'http://8.138.5.119:8088')
          })
        }
      },
    },  
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
})